﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Net.Http;
using System.IO;
using System.Web.Http;
using ExpenseSystem.Core;
using ExpenseSystem.Core.Authority;
using ExpenseSystem.Core.Converter;
using ExpenseSystem.Core.ServiceOrderModules;
using ExpenseSystem.Fether;
using ExpenseSystem.Common;
using ExpenseSystem.Lor;
using ExpenseSystem.Model;
using ExpenseSystem.Common.BaseModel;

namespace ExpenseSystem.Controllers
{
    [RoutePrefix("api/ServiceOrder")]
    public class ServiceOrderController : ApiController
    {
        SetInhibition setInhibition;
        GetInhibition getInhibition;
        SetInhibitionMaker inhibitionMaker = new StateInhibitionMaker();
        GetInhibitionMaker getInhibitionMaker = new GetInhibitionMaker();
        public ServiceOrderController()
        {
            setInhibition = inhibitionMaker.Make();
            getInhibition = getInhibitionMaker.Make();
        }

        ServiceOrderFetcher fetcher = new ServiceOrderFetcher();
        ServiceOrderDetailFetcher dfetcher = new ServiceOrderDetailFetcher();

        ServiceOrderManager manager = ServiceOrderManager.CreateInstance();
        ServiceOrderDetailManager dmanager = ServiceOrderDetailManager.CreateInstance();

        [Route("List")]
        [HttpGet]
        public ResponseModel List([FromUri]RequestArgs paramModels)
        {
            return NormalControllerHelper.List(fetcher, "SAM_SERVICEORDER_LIST", paramModels, getInhibition);
        }
        [Route("Get")]
        [HttpGet]
        public ResponseModel Get(long id)
        {
            return NormalControllerHelper.Get(fetcher, "SAM_SERVICEORDER_LIST", id, getInhibition);
        }

        [Route("Set")]
        [HttpPost]
        public ResponseModel Set(ServiceOrderModel data)
        {
            return NormalControllerHelper.Set(fetcher, "SAM_SERVICEORDER_SET", data, manager, setInhibition, getInhibition);
        }

        [Route("Remove")]
        [HttpPost]
        public ResponseModel Remove(ServiceOrderModel data)
        {
            return NormalControllerHelper.Remove("SAM_SERVICEORDER_DEL", data, manager);
        }

        [Route("RemoveById")]
        [HttpGet]
        public ResponseModel RemoveById([FromUri]long[] id)
        {
            return NormalControllerHelper.RemoveById("SAM_SERVICEORDER_DEL", id, manager);
        }

        [Route("ListDetails")]
        [HttpGet]
        public ResponseModel ListDetails(long orderid)
        {
            return NormalControllerHelper.ListDetail(dfetcher, "SAM_SERVICEORDER_LIST", orderid, getInhibition);
        }

        [Route("SetDetails")]
        [HttpPost]
        public ResponseModel SetDetails([FromBody] RequestModel<ServiceOrderDetailModel> requestData)
        {
            ResponseModel response = new ResponseModel();
            for (int i = 0; i < requestData.Data.Count(); i++)
            {
                if (requestData.Data[i].ServiceId != requestData.MasterId)
                {
                    requestData.Data[i].ServiceId = requestData.MasterId;
                }
            }
            return NormalControllerHelper.SetDetail(dfetcher, dfetcher, "SAM_SERVICEORDER_SET", requestData, dmanager, setInhibition);
        }
    }
}
